# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=lua-xml
_rockname=xml
pkgver=1.1.3
pkgrel=2
pkgdesc="Very fast XML parser for Lua based on RapidXML"
url="https://github.com/lubyk/xml"
arch="all"
license="MIT"
depends="lua-lub"
checkdepends="lua-lut luajit"
source="https://github.com/lubyk/$_rockname/archive/REL-$pkgver/lua-xml-$pkgver.tar.gz
	dub-update-to-C++17-style-exception-notation.patch"
builddir="$srcdir/$_rockname-REL-$pkgver"

case "$CARCH" in
ppc64le|riscv64|loongarch64)
	# blocked by luajit
	options="$options !check"
	;;
esac

_luaversions="5.1 5.2"
for _v in $_luaversions; do
	makedepends="$makedepends lua$_v-dev luarocks$_v"
	subpackages="$subpackages lua$_v-${pkgname#lua-}:_subpackage"
done

build() {
	local lver; for lver in $_luaversions; do
		msg "Building for Lua $lver..."

		luarocks-$lver \
			CC="$CC" \
			CFLAGS="$CFLAGS -fPIC" \
			LUA_INCDIR="$(pkg-config --variable=includedir lua$lver)" \
			LUA_LIBDIR="$(pkg-config --variable=libdir lua$lver)" \
			make --tree=./build-$lver \
			$_rockname-$pkgver-*.rockspec
	done
}

check() {
	local lver; for lver in $_luaversions jit; do
		msg "Testing on lua$lver"

		ln -sf ../build-${lver/jit/5.1}/lib/lua/${lver/jit/5.1}/xml/core.so xml/
		lua$lver test/all.lua
	done
}

package() {
	mkdir -p "$pkgdir"
}

_subpackage() {
	local lver="${subpkgname:3:3}"
	pkgdesc="$pkgdesc (for Lua $lver)"
	depends="$depends lua$lver"
	install_if="$pkgname=$pkgver-r$pkgrel lua$lver"
	local rocksdir="$subpkgdir/usr/lib/luarocks/rocks-$lver"

	mkdir -p "$subpkgdir"/usr
	cp -r "$builddir"/build-$lver/* "$subpkgdir"/usr/

	rm -r "$rocksdir"/manifest "$rocksdir"/$_rockname/*/doc
}

sha512sums="
fdbfab6aaf46e71a02a2ac6dd0b3f708ee7e712f9704a2b834c2240696519dbcbb28fcee555bd6f12f992ee8713597aaee4b7057efcc1c004303bbe0f2223776  lua-xml-1.1.3.tar.gz
a7e83866c7af7d1e6c08c32891b5749f816d8024036de1976c0d5dd9cea09e619c07176f066066a837f984f3a2cdd245ffb9d55d57879eb98d5cd130f71a9370  dub-update-to-C++17-style-exception-notation.patch
"
